import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Post } from './post.entity';
import { UserSys } from 'src/user/userSys.entity';

@Injectable()
export class PostService {
    constructor(
        @InjectRepository(Post)
        public readonly post: Repository<Post>
    ) { }

    async getCurrent(user: UserSys) {
        const hash_key = Post.getCurrentHashKey(user)
        const entity = await this.post.findOne({
            where: {
                hash_key
            }
        })
        const yesterday = await this.post.findOne({
            where: {
                hash_key: Post.getYesterday(user)
            }
        })
        if (!!entity) {
            entity.plan_today = yesterday?.plan_tomorrow
            return entity;
        }
        const result = this.post.create({
            hash_key,
            user
        });
        result.plan_today = yesterday?.plan_tomorrow
        return result;
    }
}
